package tutorial.api.new_api.package_again;

import org.apache.catalina.util.ParameterMap;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import public_util.model.Const4PublicDataBase;
import public_util.model.ReturnData;
import public_util.servlet.ServletParent;
import system_api_4_trade.tool_4_servlet.ToolServlet4PublicCustomer4GloabalMgr;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * api重新打包, 既可以利用本地的逻辑内容，也可以兼顾外部的格式要求
 */

@Controller("/application_4_tool_api")
public class PackageApiServlet extends ServletParent {

    private static final long serialVersionUID = 1L;
    private static Logger logger = Logger.getLogger(PackageApiServlet.class);

    /**
     * @see HttpServlet#HttpServlet()
     */
    public PackageApiServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }



    /**
     */
    @RequestMapping("/application_4_tool_api")
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String methodName = request.getParameter(Const4PublicDataBase.PARA_invoke_method);
        if (methodName!= null)
        {
            logger.info("--2--");
            super.doPost(request, response);
            return;
        }
        logger.info("--1--");

        //--提前打印出来
        ReturnData data  = new ReturnData();

        data.setStatus(0);
        printRes(response, data);
    }




    /**
     * 测试URL: application_4_tool_api?invoke_method=get_cookie_id_4_app
     * 将maps解锁后,后面可以塞入相关的值,完成参数透传
     */
    public void get_cookie_id_4_app(HttpServletRequest request, HttpServletResponse response) {

        ParameterMap<String, String[]> maps = (ParameterMap<String, String[]>)request.getParameterMap();
        maps.setLocked(false);//此处必须解锁, 否则报错, put不了值
        maps.put("pre_cmd_para_pass_way_type", new String[]{"only_cookie"});
        maps.put("jump_type", new String[]{"999"});
        ReturnData returnData = ToolServlet4PublicCustomer4GloabalMgr.public_page_4a_login_jump(getSqlSession(), this, request.getSession(), response, request);
        if (returnData.getStatus() != 0)
        {
            printRes(response, returnData);
            return;
        }
        printRes(response, returnData);
    }


}
